package icu.bughub.app.app.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.pager.VerticalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import icu.bughub.app.app.viewmodel.MainViewModel
import kotlinx.coroutines.launch
import java.util.Timer
import java.util.TimerTask

/**
 * @author huangzehui
 * @date 25/5/2024 上午12:58 周六
 * @description 通知内容
 *
 * https://www.bilibili.com/video/BV1aS4y1D7dv?p=11&spm_id_from=pageDriver&vd_source=e07cc8cc94eb22bd76722071b03cd199
 */


@Composable
fun NotificationContent(vm: MainViewModel) {

    // 为了使轮播图可以无限循环，使用虚拟页数
    // 实际页数
    val actualCount = vm.notifications.size

    // 图片个数是否小于2
    val isLess2 = actualCount < 2

    // 虚拟页数
    val virtualCount = if (isLess2) actualCount else Int.MAX_VALUE

    // 初始图片下标
    val initialIndex = virtualCount / 2

    Row(
        modifier = Modifier
            .padding(8.dp)
            .clip(RoundedCornerShape(8.dp))
            .background(Color(0x22149EE7))
            .height(45.dp)
            .padding(horizontal = 8.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween,
    ) {


        Text(
            text = "最新活动",
            color = Color(0xFF149EE7),
            fontSize = 14.sp
        )

        Spacer(modifier = Modifier.width(8.dp))

        val state = rememberPagerState(pageCount = {
            virtualCount
        }, initialPage = initialIndex)

        val coroutineScope = rememberCoroutineScope()

        DisposableEffect(key1 = Unit) {
            val timer = Timer()

            timer.schedule(object : TimerTask() {

                override fun run() {
                    coroutineScope.launch {
                        state.animateScrollToPage(state.currentPage+1)
                    }
                }

            }, 3000, 3000)

            onDispose {
                timer.cancel()
            }
        }

        VerticalPager(
            state = state,
            modifier = Modifier.weight(1f),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) { index ->
            val actualIndex = index % actualCount
            Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center) {
                Text(
                    text = vm.notifications[actualIndex],
                    color = Color(0xFF333333),
                    fontSize = 14.sp,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                )
            }
        }

        Text(
            text = "更多",
            color = Color(0xFF666666),
            fontSize = 14.sp,
            maxLines = 1,
        )

    }
}
